<template>
    <div>
        房价名称<input type="text" v-model="queryinfo.planname" @click="show"/>
        <input type="button" value="批量修改" class="btn btn-success" @click="updateall"/>
        <input type="button" value="批量删除" class="btn btn-danger" @click="delall"/>
        <input type="button" value="导出" class="btn btn-warning" @click="expor"/>
        <input type="button" value="导入" class="btn btn-danger" @click="delall"/>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <input type="checkbox" v-model="checked" @click="checkall"/>
                    <td>房价计划名称</td>
                    <td>门店名称</td>
                    <td>房型名称</td>
                    <td>房型价格</td>
                    <td>数量</td>
                    <td>状态</td>
                    <td>早餐数量</td>
                    <td>开始时间</td>
                    <td>结束时间</td>
                    <td>操作</td> 
                </tr>
                <tr v-for="item in data.list">
                    <input type="checkbox" v-model="ids" :value="item.Id" @change="checkone"/>
                    <td>{{ item.PlanName }}</td>
                    <td>{{ item.ShopName }}</td>
                    <td>{{ item.HouseTypeName }}</td>
                    <td>{{ item.Price }}</td>
                    <td>{{ item.Count }}</td>
                    <td>{{ item.State?"启用":"禁用" }}</td>
                    <td>{{ item.State?"有早餐":"无早餐" }}</td>
                    <td>{{ item.BreakFastCount }}</td>
                    <td>{{ item.BeginTime.substring(0,10) }}</td>
                    <td>{{ item.EndTime.substring(0,10) }}</td>
                    <td>
                        <input type="button" value="修改" class="btn btn-success" @click="upd(item.Id)"/>
                        <input type="button" value="删除" class="btn btn-danger" @click="del(item.Id)"/>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <span>共{{ data.topage }}页</span>
            <span>共{{ data.tocount }}条</span>
            <span>当前{{ queryinfo.pageindex }}页</span>
            <select v-model="queryinfo.pagesize" @change="show">
                <option :value="item" v-for="item in arr">每页{{ item }}条</option>
            </select>
            <input type="button" value="首页" @click="fanye(1)" class="btn btn-info"/>
            <input type="button" value="上一页" @click="fanye(queryinfo.pageindex-1)" class="btn btn-info"/>
            <input type="button" value="下一页"  @click="fanye(queryinfo.pageindex+1)" class="btn btn-info"/>
            <input type="button" value="尾页" @click="fanye(data.topage)" class="btn btn-info"/>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive, onMounted}from 'vue';
import axios from 'axios';
import moment from 'moment';
import {useRouter,useRoute}from 'vue-router'
const router=useRouter();
const route=useRoute();

let checked:any=ref([false]);
let ids=ref([]);

let queryinfo=reactive({
    pageindex:1,
    pagesize:2,
    planname:""
})

let arr:any=ref([2,5,6])

let data:any=reactive({
    list:[],
    tocount:0,
    topage:0,
})
onMounted(()=>{
    getlist();
})

const expor=()=>{
    location.hash=`https://localhost:7178/api/House/ExProExcel`;
}

const delall=()=>{
    if(!confirm("确认删除吗")){
        return;
    }
    axios({
        url:'/api/House/DelPrice',
        method:'delete',
        params:{ids:ids.value.toString()}
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('删除成功');
            checked.value=false;
            getlist();
        }
        else{
            alert('删除失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const updateall=()=>{
    if(!confirm("确认修改吗")){
        return;
    }
    axios({
        url:'/api/House/Getstateprice',
        method:'get',
        params:{ids:ids.value.toString()}
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('修改成功');
            checked.value=false;
            getlist();
        }
        else{
            alert('修改失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const checkall=()=>{
    checked.value=!checked.value;
    if(checked.value==true){
        ids.value=data.list.map((item:any)=>{
            return item.Id;
        })
    }
    else{
        return ids.value=[];
    }
}

const checkone=()=>{
    if(ids.value.length==data.list.length){
        checked.value=true;
    }
    else{
        checked.value=fanye;
    }
}

const del=(id:any)=>{
    if(!confirm("确认删除吗")){
        return;
    }
    axios({
        url:'/api/House/DelPrice',
        method:'delete',
       params:{id:id}
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('删除成功');
            router.push('/housepricelist')
        }
        else{
            alert('删除失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const upd=(id:any)=>{
    router.push({
        path:'/housepriceupdate',
        query:{id:id}
    })
}

const show=()=>{
    queryinfo.pageindex=1;
    getlist();
}
const fanye=(num:any)=>{
    if(num<1){
        queryinfo.pageindex=1;
        return;
    }
    if(num>data.topage){
        queryinfo.pageindex=data.topage;
        return;
    }
    queryinfo.pageindex=num;
    getlist();
}

const getlist=()=>{
    axios({
        url:'/api/House/GetHousePrice',
        method:'get',
        params:queryinfo
    })
    .then(res=>{
        console.log(res);
        data.list=res.data.list;
        data.tocount=res.data.tocount;
        data.topage=res.data.topage;
    })
    .catch(err=>{
        console.log(err);
    })
}
</script>

<style scoped>
input[type="button"]{
    margin-left: 10px;
}
span{
    margin-left: 10px;
}
</style>